import {
	TT_PAY_STATUS,
} from '@/const/index.js';
import {
	bindPetTiktokOrderId,
} from '@/api/pet-core.js';
import {
	bindShopTiktokOrderId,
} from '@/api/shop-activity.js';
import {
	ORDER_TYPE,
} from '@/const/index.js';
export function ttPayOld(sn, orderId, orderToken) {
	return new Promise((resolve, reject) => {
		uni.getProvider({
			service: 'payment',
			success: (res) => {
				console.log('getProvider', res);
				const providers = res.provider;
				if (providers && providers.length > 0) {
					uni.requestPayment({
						provider: providers[0],
						orderInfo: {
							order_id: orderId,
							order_token: orderToken,
						},
						service: 5,
						success: function(res) {
							console.log(res);
							if (res.code === TT_PAY_STATUS.SUCCESS) {
								resolve({
									code: TT_PAY_STATUS.SUCCESS,
									sn,
								});
							} else if (res.code === TT_PAY_STATUS.PAY_CLOSE) {

								uni.showToast({
									title: '支付关闭',
									icon: 'none',
								});
								resolve({
									code: TT_PAY_STATUS.PAY_CLOSE,
								});
							} else if (res.code === TT_PAY_STATUS.PAY_CANCEL) {
								uni.showToast({
									title: '已取消支付',
									icon: 'none',
								});
								resolve({
									code: TT_PAY_STATUS.PAY_CANCEL,
								});
							} else if (res.code === TT_PAY_STATUS.PAY_FAIL) {
								uni.showToast({
									title: '支付失败，请重新支付',
									icon: 'none',
								});
								resolve({
									code: TT_PAY_STATUS.PAY_FAIL,
								});
							} else if (res.code === TT_PAY_STATUS.PAY_TIMEOUT) {
								uni.showToast({
									title: '支付超时，请重新支付',
									icon: 'none',
								});
								resolve({
									code: TT_PAY_STATUS.PAY_TIMEOUT,
								});
							} else {
								uni.showToast({
									title: res.errMsg,
									icon: 'none',
								});
							}
						},
						fail: function(err) {
							console.log('fail:' + JSON.stringify(err));
						},
					});
				}
			},
		});
	});
}

export function ttPay(sn, orderData, sign, type = ORDER_TYPE.PET) {
	return new Promise((resolve, reject) => {
		tt.requestOrder({
			data: orderData,
			byteAuthorization: sign,
			success: res => {
				if (res.errMsg === 'requestOrder:ok') {
					if (type === ORDER_TYPE.MALL) {
						console.log('res.orderId', sn);
						bindShopTiktokOrderId({
							orderId: res.orderId,
						}, sn);
					} else {
						bindPetTiktokOrderId({
							orderId: res.orderId,
						}, sn);
					}

					startPay(res.orderId, sn).then(res => {
						resolve(res);
					});
				}
			},
			fail: err => {
				console.log('requestOrder', err);
				resolve({
					code: TT_PAY_STATUS.PAY_FAIL,
					msg: '支付失败，请重试',
				});

			},
		});
	});
}

export function startPay(orderId, sn) {
	return new Promise((resolve, reject) => {
		tt.getOrderPayment({
			orderId: orderId,
			success: payRes => {
				if (payRes.errMsg === 'getOrderPayment:ok') {
					resolve({
						code: TT_PAY_STATUS.SUCCESS,
						sn,
					});
				}
			},
			fail: payErr => {
				console.log('requestOrder', payErr, handlerPayErr(payErr.errNo));
				resolve({
					code: TT_PAY_STATUS.PAY_FAIL,
					msg: handlerPayErr(payErr.errNo),
				});

			},
		});
	});
}

function handlerPayErr(errCode) {
	switch (errCode) {
		case 1:
			return '支付超时';
		case 3:
		case 4:
			return '支付取消';
		default:
			return '支付失败，请重试';
			// case 10401:
			//   return '请求异常，可重试或升级APP';
			// case 11001:
			//   return '访问未授权';
			// case 12002:
			//   return '账号行为异常';
			// case 10000:
			//   return '参数错误';
			// case 20000:
			//   return '订单不存在';
			// case 21012:
			//   return '创建订单失败，请稍后重试';
	}
}